package mh;

import java.util.ArrayList;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import de.matthiasmann.twl.Event;
import de.matthiasmann.twl.FPSCounter;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.RadialPopupMenu;
import de.matthiasmann.twl.ToggleButton;
import de.matthiasmann.twl.WheelWidget;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.model.SimpleChangableListModel;
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
import de.matthiasmann.twl.theme.ThemeManager;

public class GameUIDemo extends Widget {

	public static void main(String[] args) {
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
			Display.setTitle("TWL Game UI Demo");
			Display.setVSyncEnabled(true);

			LWJGLRenderer renderer = new LWJGLRenderer();
			GameUIDemo gameUI = new GameUIDemo();
			GUI gui = new GUI(gameUI, renderer);

			ThemeManager theme = ThemeManager.createThemeManager(GameUIDemo.class.getResource("gameui.xml"), renderer);
			gui.applyTheme(theme);

			while (!Display.isCloseRequested() && !gameUI.quit) {
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

				gui.update();
				Display.update();
				//TestUtils.reduceInputLag();
			}

			gui.destroy();
			theme.destroy();
		} catch (Exception ex) {
			//TestUtils.showErrMsg(ex);
			ex.printStackTrace();
		}
		Display.destroy();
	}

	private final ToggleButton[]					actionButtons;
	private final ToggleButton						btnPause;
	private final ToggleButton						btnArmageddon;
	private final FPSCounter						fpsCounter;
	private final Label								lastSelectedRadialEntry;

	private final SimpleChangableListModel<String>	digits;
	private final ArrayList<WheelWidget<String>>	wheels;

	public boolean									quit;

	private static final String[]					ACTION_NAMES	= { "pingu-digger", "pingu-miner", "pingu-basher", "pingu-climber", "pingu-floater",
			"pingu-bomber", "pingu-blocker", "pingu-bridger",		};

	public GameUIDemo() {
		this.actionButtons = new ToggleButton[ACTION_NAMES.length];
		for (int i = 0; i < ACTION_NAMES.length; i++) {
			this.actionButtons[i] = new ToggleButton();
			this.actionButtons[i].setTheme(ACTION_NAMES[i]);
			this.add(this.actionButtons[i]);
		}

		this.btnPause = new ToggleButton();
		this.btnPause.setTheme("pause");
		this.add(this.btnPause);

		this.btnArmageddon = new ToggleButton();
		this.btnArmageddon.setTheme("armageddon");
		this.add(this.btnArmageddon);

		this.fpsCounter = new FPSCounter(3, 0);
		this.add(this.fpsCounter);

		this.lastSelectedRadialEntry = new Label();
		this.lastSelectedRadialEntry.setText("Right click on the background");
		this.add(this.lastSelectedRadialEntry);

		this.digits = new SimpleChangableListModel<String>("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
		this.wheels = new ArrayList<WheelWidget<String>>();
		for (int i = 0; i < 4; i++) {
			WheelWidget<String> wheel = new WheelWidget<String>(this.digits);
			this.wheels.add(wheel);
			wheel.setCyclic(true);
			this.add(wheel);
		}
	}

	@Override
	protected void layout() {
		int x = 10;
		int y = 40;

		for (ToggleButton b : this.actionButtons) {
			b.setPosition(x, y);
			b.adjustSize();
			y += b.getHeight() + 5;
		}

		x = this.getInnerWidth() - 10;
		y = 10;

		this.btnPause.adjustSize();
		x -= this.btnPause.getWidth() + 5;
		this.btnPause.setPosition(x, y);

		this.btnArmageddon.adjustSize();
		x -= this.btnArmageddon.getWidth() + 5;
		this.btnArmageddon.setPosition(x, y);

		this.fpsCounter.adjustSize();
		this.fpsCounter.setPosition(this.getInnerWidth() - this.fpsCounter.getWidth(), this.getInnerHeight() - this.fpsCounter.getHeight());

		this.lastSelectedRadialEntry.adjustSize();
		this.lastSelectedRadialEntry.setPosition((this.getInnerWidth() / 2) - (this.lastSelectedRadialEntry.getWidth() / 2), this.getInnerBottom()
				- this.lastSelectedRadialEntry.getHeight());

		int wheelsWidth = 0;
		for (WheelWidget<String> wheel : this.wheels) {
			wheel.adjustSize();
			wheelsWidth += wheel.getWidth();
		}
		x = this.getInnerX() + ((this.getInnerWidth() - wheelsWidth) / 2);
		y = this.getInnerY() + ((this.getInnerHeight() - this.wheels.get(0).getHeight()) / 2);
		for (WheelWidget<String> wheel : this.wheels) {
			wheel.setPosition(x, y);
			x += wheel.getWidth();
		}
	}

	@Override
	protected boolean handleEvent(Event evt) {
		if (super.handleEvent(evt)) {
			return true;
		}
		switch (evt.getType()) {
			case KEY_PRESSED:
				switch (evt.getKeyCode()) {
					case Event.KEY_ESCAPE:
						this.quit = true;
						return true;
				}
				break;
			case MOUSE_BTNDOWN:
				if (evt.getMouseButton() == Event.MOUSE_RBUTTON) {
					return this.createRadialMenu().openPopup(evt);
				}
				break;
		}
		return evt.isMouseEventNoWheel();
	}

	RadialPopupMenu createRadialMenu() {
		RadialPopupMenu rpm = new RadialPopupMenu(this);
		for (int i = 0; i < 10; i++) {
			final int idx = i;
			rpm.addButton("star", new Runnable() {
				@Override
				public void run() {
					GameUIDemo.this.lastSelectedRadialEntry.setText("Selected " + idx);
				}
			});
		}
		return rpm;
	}
}
